package orchestration.android.execution.commands;

import orchestration.android.RunExecutionActivity;
import orchestration.android.execution.exceptions.TaskExecutionException;
import orchestration.android.execution.tasks.AndroidTask;
import orchestration.android.execution.tasks.AndroidTaskFactory;
import orchestration.android.parcelable.ParcelableAndroidTaskDTO;
import orchestration.persistence.exceptions.MalformedAnnotationException;

/**
 * An {@link AndroidOrchestrationCommand} that executes tasks
 * @author  <a href="mailto:car.ferrarini@gmail.com">Carlo Ferrarini</a>
 */
public class ExecTaskCommand implements AndroidOrchestrationCommand {

	/**
	 * @uml.property  name="dto"
	 * @uml.associationEnd  
	 */
	private ParcelableAndroidTaskDTO dto;

	public ExecTaskCommand(ParcelableAndroidTaskDTO dto) {
		this.dto=dto;
	}

	@Override
	public void execute(RunExecutionActivity rea) {
		AndroidTask task=null;
		try {
			task = AndroidTaskFactory.createAndroidTask(dto);
		} catch (MalformedAnnotationException e) {
			rea.handleException(e);
		}
		if(task!=null){
			rea.setCurrentTask(task);
			try {
				task.onExecute(rea);
			} catch (TaskExecutionException e) {
				rea.handleException(e);
			}
		}
	}

	@Override
	public int getId() {
		return CommandFactory.EXEC_TASK;
	}

}
